import DistrictPicker from '@/components/DistrictPicker'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
import orderService from '@/api/orderService'
import util from '@/utils/util'
import { forEach, map } from 'lodash'
export default {
  equipmentCoding: 'Order',
  components: {
    DistrictPicker,
    Pagination
  },
  computed: {},
  data() {
    return {
      searchParams: {
        orderNO:'',
        customerCode:'',
        orderType:'',
        remark:'',
        start_AppointmentTime:'',
        end_AppointmentTime:'',
        start_CreateTime:'',
        end_CreateTime:'',
        province: '',
        city: '',
        district: '',
        area: '',
        address:'',
        pageIndex: 1,
        pageSize: 10,
        totalCount: 0
      },
      checkEmployeForm: {},
      audit:{},
      searchLoading: false,
      activeName: 'All',
      assignFormVisible: false,
      auditFormVisible:false,
      tableKey: 0,
      total: 2,
      tableLoading: false,
      selectedsOrders: [],
      tableData: [],
      employees: [],
      assignLoading: false,
      auditLoading: false,
      orderStateInfo: {},
      orderTypes:[{
        value:0,
        label:'扔垃圾'
      },{
        value:1,
        label:'取快递'
      }],
      orderStatues: [
        {
          value: 1,
          label: '已下单'
        }, {
          value: 2,
          label: '已支付'
        }, {
          value: 3,
          label: '已接收'
        }, {
          value: 4,
          label: '配送中'
        },{
          value: 5,
          label: '已完成'
        },{
          value: 6,
          label: '已取消'
        }],
      workOrderTypes: [
        {
          value: '0',
          label: '小程序'
        }, {
          value: '1',
          label: 'PC'
        }]
    }
  },
  created() {
    this.searchOrders();
  },
  methods: {
    querySearchSiteAsync(queryString, cb) {
      let params = {
        fieldName: queryString,
        pageIndex: 1,
        pageSize: 10
      }
    //   siteSevice.searchSite(params).then(response => {
    //     let fields = [];
    //     forEach(response.fields, field => {
    //       fields.push({ id: field.id, value: field.fieldName, address: field.position })
    //     })
    //     cb(fields);
    //   }, error => {
    //     util.processErrorResponse(error)
    //   })
    },
    querySearchDeviceAsync(queryString, cb) {
      let params = {
        deviceName: queryString,
        pageIndex: 1,
        pageSize: 10
      }
    //   equipmentSevice.searchEquipment(params).then(response => {
    //     let devices = [];
    //     forEach(response.devices, device => {
    //       devices.push({ id: device.id, value: device.deviceName, address: device.positionExplain })
    //     })
    //     cb(devices);
    //   }, error => {
    //     util.processErrorResponse(error)
    //   })
    },
    querySearchEmployeeAsync(queryString, cb) {
      let params = {
        employeeName: queryString,
        pageIndex: 1,
        pageSize: 10
      }
      // employeeService.searchEmployee(params).then(response => {
      //   cb(response.employees);
      // }, error => {
      //   util.processErrorResponse(error)
      // })
    },
    selectEmployee(item) {
      this.checkEmployeForm.checkEmployeeId = item.id;
    },
    /**
     * 下载
     */
    download() {

    },
    /**
     * 查询订单
     */
    searchOrders() {
      this.searchLoading = true;
      this.tableLoading = true;
      orderService.getOrders(this.searchParams).then(response =>{
        this.tableData = response.orders
        this.searchLoading = false;
        this.tableLoading = false;
        this.searchParams.totalCount = response.count;

        this.orderStateInfo.allStateTotal = 0;
        forEach(response.orderStatusNums, info => {
          if (info.orderStatusId === '1') {
            this.orderStateInfo.submitted = info.orderNum;
          }
          else if (info.orderStatusId === '2') {
            this.orderStateInfo.paid = info.orderNum;
          }
          else if (info.orderStatusId === '3') {
            this.orderStateInfo.receiveConfirmed = info.orderNum;
          }
          else if (info.orderStatusId === '4') {
            this.orderStateInfo.delivery = info.orderNum;
          }
          else if (info.orderStatusId === '5') {
            this.orderStateInfo.completed = info.orderNum;
          }
          else if (info.orderStatusId === '6') {
            this.orderStateInfo.beenRepairTotal = info.orderNum;
          }
          this.orderStateInfo.cancelled += info.orderNum
        })
      },err =>{
         this.searchLoading = false;
         this.tableLoading = false;
         util.processErrorResponse(error)
      })  
    },
    onSelectedPCD(proCityArea) {
      let province = proCityArea.province;
      let city = proCityArea.city;
      let area = proCityArea.area;
      this.searchParams.province = '';
      this.searchParams.city = '';
      this.searchParams.area = '';
      this.searchParams.district = '';
      if (province.code) {
        this.searchParams.province = province.value;
      }
      if (city.code) {
        this.searchParams.city = city.value;
      }
      if (area.code) {
        this.searchParams.district = this.searchParams.area = area.value;
      }
    },
    pageChange(pager) {
      this.searchParams.pageSize = pager.limit;
      this.searchParams.pageIndex = pager.page;
      this.searchOrders();
    },
    resetOrder() {
      this.searchParams = {
        province: '',
        city: '',
        district: '',
        area: '',
        pageIndex: 1,
        pageSize: 10,
        totalCount: 0
      };
    },
    /**
     *客户明细
     */
    customerDetail(row){
        // this.$router.push({
        //     path: `/work-order/detail/${row.id}`
        //   })
    },
    /**
     * 查看订单
     */
    orderDetail(row) {
        // this.$router.push({
        //     path: `/work-order/detail/${row.id}`
        //   })
    //   this.auditFormVisible =true;
    //   this.audit ={};
    //   this.audit.workOrderId = row.id;
      // MessageBox.confirm('工单取消后不可恢复,确认删除吗?', '提示', {
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      //   workOrderSevice.cancelWorkOrder({ workOrderId: row.id }).then(() => {
      //     util.success("工单取消成功");
      //     this.searchOrders();
      //   }, error => {
      //     util.processErrorResponse(error)
      //   })
      // }).catch(() => {

      // });

    },
    auditSubmit(){
        // workOrderSevice.cancelWorkOrder(this.audit).then(() => {
        //   util.success("工单取消成功");
        //   this.auditFormVisible =false;
        //   this.searchOrders();
        // }, error => {
        //   util.processErrorResponse(error)
        // })
    },
    selectionCheckBoxChange(order) {
      this.selectedsOrders = order;
    },
    assignWorkOrderWithCheckBox() {
      if (this.selectedsOrders.length == 0) {
        this.$message({
          message: '请选择需要指派工单',
          type: 'warning',
          showClose: true,
        });
        return;
      }
      this.assignFormVisible = true;
    },
    onClickStatuTab(tagName, workOrderStatusId) {
      this.activeName = tagName;
      delete this.searchParams.workOrderStatusId;
      if (tagName != "All") {
        this.searchParams.workOrderStatusId = workOrderStatusId;
      }
      this.searchOrders();
    },
    /**
     * 取消订单
    */
    cancelOrderShow(row) {
    //   this.checkEmployeForm.workOrderIds = [row.id.toString()]
    //   this.assignFormVisible = true;
    },
    cancelAudit(){
      this.audit ={};
      this.auditFormVisible =false;
    },
    /**
     * 编辑订单
     */
    editOrderShow(row) {
      this.checkEmployeForm.workOrderIds = [row.id.toString()]
      this.checkEmployeForm.isReAssign= true;
      this.assignFormVisible = true;
    },
    assignSubmit() {
      this.$refs["checkEmployeForm"].validate((valid) => {
        if (valid) {
          this.assignLoading =true;
          if (this.selectedsOrders.length > 0) {
            //this.checkEmployeForm.workOrderIds = map(this.selectedsOrders, 'id');
          }
        //   if(this.checkEmployeForm.isReAssign){
        //     workOrderSevice.reAssignWorkOrder(this.checkEmployeForm).then(() => {
        //       this.assignLoading =false;
        //       this.searchOrders();
        //       this.cancelAssign();
        //     }, error => {
        //       this.cancelAssign();
        //       this.assignLoading =false;
        //       util.processErrorResponse(error)
        //     })
        //   }else{
        //     workOrderSevice.assignWorkOrder(this.checkEmployeForm).then(() => {
        //       this.assignLoading =false;
        //       this.searchOrders();
        //       this.cancelAssign();
        //     }, error => {
        //       this.cancelAssign();
        //       this.assignLoading =false;
        //       util.processErrorResponse(error)
        //     })
        //   }

        }
      });
    },
    cancelAssign() {
      this.$refs["checkEmployeForm"].resetFields();
      this.checkEmployeForm={};
      this.assignFormVisible = false;
    }
  }
}
